package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangTreasureData;
import com.yanqu.road.entity.kuang.battle.CrossKuangTreasureDetailData;
import com.yanqu.road.entity.kuang.battle.CrossKuangTreasureNoticeData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangTreasureNoticeDataDaoImpl extends TempDataObjectDao<CrossKuangTreasureNoticeData> {

    public static String getCrossKuangTreasureNoticeDataSql = "select * from `t_c_kuang_treasure_notice_data` where `weekId`=? and `groupId`=? order by `openTime` asc";

    public Map<Integer, List<CrossKuangTreasureNoticeData>> getCrossKuangTreasureNoticeDataMap(int weekId, int groupId) {
        Map<Integer, List<CrossKuangTreasureNoticeData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangTreasureNoticeDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangTreasureNoticeData data = parse(rs);
                List<CrossKuangTreasureNoticeData> list = map.getOrDefault(data.getCampId(), new ArrayList<>());
                list.add(data);
                map.put(data.getCampId(), list);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossKuangTreasureNoticeDataSql = "insert into `t_c_kuang_treasure_notice_data`(weekId,groupId,campId,round,reward,userId,userBaseInfo,openTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossKuangTreasureNoticeData crossKuangTreasureNoticeData) {
        crossKuangTreasureNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureNoticeData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureNoticeData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureNoticeData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureNoticeData.getRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossKuangTreasureNoticeData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangTreasureNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangTreasureNoticeData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, crossKuangTreasureNoticeData.getOpenTime()));
        if (getDb().executeNoneQuery(insertCrossKuangTreasureNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected boolean updateData(CrossKuangTreasureNoticeData data) {
        return true;
    }

    @Override
    protected CrossKuangTreasureNoticeData parseData(ResultSet rs) throws SQLException {
        CrossKuangTreasureNoticeData crossKuangTreasureNoticeData = new CrossKuangTreasureNoticeData();
        crossKuangTreasureNoticeData.setWeekId(rs.getInt("weekId"));
        crossKuangTreasureNoticeData.setGroupId(rs.getInt("groupId"));
        crossKuangTreasureNoticeData.setCampId(rs.getInt("campId"));
        crossKuangTreasureNoticeData.setRound(rs.getInt("round"));
        crossKuangTreasureNoticeData.setReward(rs.getString("reward"));
        crossKuangTreasureNoticeData.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        if (!StringUtils.isNullOrEmpty(userBaseInfo)) {
            crossKuangTreasureNoticeData.setUserBaseInfo(JSON.parseObject(userBaseInfo, new TypeReference<UserBaseInfo>() {
            }));
        }
        crossKuangTreasureNoticeData.setOpenTime(rs.getLong("openTime"));
        return crossKuangTreasureNoticeData;
    }
}
